package com.jianwei.web.util;

import java.security.Key;
import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

import javax.crypto.Cipher;




public class DESUtil {
	
	private static String strDefaultKey = "national";

	private Cipher encryptCipher = null;

	private Cipher decryptCipher = null;

	/**
	 * 锟斤拷byte锟斤拷锟斤拷转锟斤拷为锟斤拷示16锟斤拷锟斤拷值锟斤拷锟街凤拷 锟界：byte[]{8,18}转锟斤拷为锟斤拷0813锟斤拷 锟斤拷public static byte[]
	 * hexStr2ByteArr(String strIn) 锟斤拷为锟斤拷锟斤拷锟阶拷锟斤拷锟斤拷
	 * 
	 * @param arrB
	 *            锟斤拷要转锟斤拷锟斤拷byte锟斤拷锟斤拷
	 * @return 转锟斤拷锟斤拷锟斤拷址锟�
	 * @throws Exception
	 *             锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿猴拷锟届常锟斤拷锟斤拷锟斤拷锟届常全锟斤拷锟阶筹拷
	 */
	public static String byteArr2HexStr(byte[] arrB) throws Exception {
		int iLen = arrB.length;
		// 每锟斤拷byte锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷鼙锟绞撅拷锟斤拷锟斤拷锟斤拷址锟侥筹拷锟斤拷锟斤拷锟斤拷锟介长锟饺碉拷锟斤拷锟斤拷
		StringBuffer sb = new StringBuffer(iLen * 2);
		for (int i = 0; i < iLen; i++) {
			int intTmp = arrB[i];
			// 锟窖革拷锟斤拷转锟斤拷为锟斤拷锟斤拷
			while (intTmp < 0) {
				intTmp = intTmp + 256;
			}
			// 小锟斤拷0F锟斤拷锟斤拷锟斤拷要锟斤拷前锟芥补0
			if (intTmp < 16) {
				sb.append("0");
			}
			sb.append(Integer.toString(intTmp, 16));
		}
		return sb.toString();
	}

	/**
	 * 锟斤拷锟斤拷示16锟斤拷锟斤拷值锟斤拷锟街凤拷转锟斤拷为byte锟斤拷锟介， 锟斤拷public static String byteArr2HexStr(byte[] arrB)
	 * 锟斤拷为锟斤拷锟斤拷锟阶拷锟斤拷锟斤拷
	 * 
	 * @param strIn
	 *            锟斤拷要转锟斤拷锟斤拷锟街凤拷
	 * @return 转锟斤拷锟斤拷锟絙yte锟斤拷锟斤拷
	 * @throws Exception
	 *             锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿猴拷锟届常锟斤拷锟斤拷锟斤拷锟届常全锟斤拷锟阶筹拷
	 * @author <a href="mailto:leo841001@163.com">LiGuoQing</a>
	 */
	public static byte[] hexStr2ByteArr(String strIn) throws Exception {
		byte[] arrB = strIn.getBytes();
		int iLen = arrB.length;

		// 锟斤拷锟斤拷锟街凤拷锟绞疽伙拷锟斤拷纸冢锟斤拷锟斤拷锟斤拷纸锟斤拷锟斤拷槌わ拷锟斤拷锟斤拷址锟斤拷瘸锟斤拷锟�
		byte[] arrOut = new byte[iLen / 2];
		for (int i = 0; i < iLen; i = i + 2) {
			String strTmp = new String(arrB, i, 2);
			arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
		}
		return arrOut;
	}

	/**
	 * 默锟较癸拷锟届方锟斤拷锟斤拷使锟斤拷默锟斤拷锟斤拷钥
	 * 
	 * @throws Exception
	 */
	public DESUtil() throws Exception {
		this(strDefaultKey);
	}

	/**
	 * 指锟斤拷锟斤拷钥锟斤拷锟届方锟斤拷
	 * 
	 * @param strKey
	 *            指锟斤拷锟斤拷锟斤拷钥
	 * @throws Exception
	 */
	public DESUtil(String strKey) throws Exception {
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
		Key key = getKey(strKey.getBytes());

		encryptCipher = Cipher.getInstance("DES");
		encryptCipher.init(Cipher.ENCRYPT_MODE, key);

		decryptCipher = Cipher.getInstance("DES");
		decryptCipher.init(Cipher.DECRYPT_MODE, key);
	}

	/**
	 * 锟斤拷锟斤拷锟街斤拷锟斤拷锟斤拷
	 * 
	 * @param arrB
	 *            锟斤拷锟斤拷艿锟斤拷纸锟斤拷锟斤拷锟�
	 * @return 锟斤拷锟杰猴拷锟斤拷纸锟斤拷锟斤拷锟�
	 * @throws Exception
	 */
	public byte[] encrypt(byte[] arrB) throws Exception {
		return encryptCipher.doFinal(arrB);
	}

	/**
	 * 锟斤拷锟斤拷锟街凤拷
	 * 
	 * @param strIn
	 *            锟斤拷锟斤拷艿锟斤拷址锟�
	 * @return 锟斤拷锟杰猴拷锟斤拷址锟�
	 * @throws Exception
	 */
	public String encrypt(String strIn) throws Exception {
		return byteArr2HexStr(encrypt(strIn.getBytes()));
	}

	/**
	 * 锟斤拷锟斤拷锟街斤拷锟斤拷锟斤拷
	 * 
	 * @param arrB
	 *            锟斤拷锟斤拷艿锟斤拷纸锟斤拷锟斤拷锟�
	 * @return 锟斤拷锟杰猴拷锟斤拷纸锟斤拷锟斤拷锟�
	 * @throws Exception
	 */
	public byte[] decrypt(byte[] arrB) throws Exception {
		return decryptCipher.doFinal(arrB);
	}

	/**
	 * 锟斤拷锟斤拷锟街凤拷
	 * 
	 * @param strIn
	 *            锟斤拷锟斤拷艿锟斤拷址锟�
	 * @return 锟斤拷锟杰猴拷锟斤拷址锟�
	 * @throws Exception
	 */
	public String decrypt(String strIn) throws Exception {
		return new String(decrypt(hexStr2ByteArr(strIn)));
	}

	/**
	 * 锟斤拷指锟斤拷锟街凤拷锟斤拷锟斤拷锟皆匡拷锟斤拷锟皆匡拷锟斤拷锟斤拷锟街斤拷锟斤拷锟介长锟斤拷为8位 锟斤拷锟斤拷8位时锟斤拷锟芥补0锟斤拷锟斤拷锟斤拷8位只取前8位
	 * 
	 * @param arrBTmp
	 *            锟斤拷锟缴革拷锟街凤拷锟斤拷纸锟斤拷锟斤拷锟�
	 * @return 锟斤拷傻锟斤拷锟皆�
	 * @throws java.lang.Exception
	 */
	private Key getKey(byte[] arrBTmp) throws Exception {
		// 锟斤拷锟斤拷一锟斤拷锟秸碉拷8位锟街斤拷锟斤拷锟介（默锟斤拷值为0锟斤拷
		byte[] arrB = new byte[8];

		// 锟斤拷原始锟街斤拷锟斤拷锟斤拷转锟斤拷为8位
		for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
			arrB[i] = arrBTmp[i];
		}

		// 锟斤拷锟斤拷锟皆�
		Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");

		return key;
	}
	private static String readString6(String prompt){
		Scanner scanner=new Scanner(System.in);
		System.out.print(prompt);
		return scanner.nextLine();
		
	}
	
	/**
	 * Main
	 */
	public static void main(String[] args) {
		try {
			
			System.out.println(Class.forName("com.sasgas.ldws.action.cfa.base.CFAfacontractAction"));
			
			DESUtil des = new DESUtil("SASGAS");
//			String str=readString6("Please Input A String: ");
//			System.out.println("Secret_Output: "+des.encrypt(str));
			
			System.out.println("锟斤拷锟杰ｏ拷"+des.encrypt("jdbc:sqlserver://172.16.110.190:1433;databaseName=LDWS_CFA"));
			System.out.println("锟斤拷锟杰ｏ拷"+des.decrypt("6d9da2fec3b76f98"));
			System.out.println("锟斤拷锟杰ｏ拷"+des.encrypt("sa"));
			System.out.println("锟斤拷锟杰ｏ拷"+des.encrypt("sasgasadmin"));
			System.out.println("锟斤拷锟杰ｏ拷"+des.encrypt("admin@123"));
			System.out.println("锟斤拷锟斤拷BMO锟斤拷锟斤拷锟斤拷锟街凤拷"+des.encrypt("jdbc:sqlserver://10.119.60.169:1433;databaseName=LDWS_NEWSG"));
			System.out.println("锟斤拷锟斤拷BMO锟斤拷锟斤拷锟斤拷锟斤拷锟诫："+des.encrypt("bmocn,001"));
			Date d = new Date();
			//date.compareTo(modifydate);
			//
			int a= d.compareTo(new SimpleDateFormat("yyyy-MM-dd").parse("2012-12-18"));
//			System.out.println(a);
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
